package com.gitee.hljdrl.dsbridge.app;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.gitee.hljdrl.dsbridge.R;
import com.gitee.hljdrl.dsbridge.basis.ViewPresenter;
import com.gitee.hljdrl.dsbridge.clean.DSViewPresenter;

public class AppWebActivity extends FragmentActivity {

    protected ViewPresenter mViewPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = getLayoutId();
        if (layoutId != 0) {
            setContentView(layoutId);
        }
        mViewPresenter = createViewPresenter();
        if (mViewPresenter != null) {
            mViewPresenter.onCreate(this);
        }
    }

    protected @LayoutRes int getLayoutId() {
        return R.layout.dsb_app_web_activity;
    }

    protected ViewPresenter createViewPresenter() {
        return new DSViewPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mViewPresenter != null) {
            mViewPresenter.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mViewPresenter != null) {
            mViewPresenter.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mViewPresenter != null) {
            mViewPresenter.onDestroy();
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        Bundle fixBundle = new Bundle();
        super.onSaveInstanceState(fixBundle);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mViewPresenter != null) {
            mViewPresenter.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (mViewPresenter != null) {
            if (mViewPresenter.onBackPressed()) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mViewPresenter != null) {
            if (mViewPresenter.onKeyDown(keyCode, event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(mViewPresenter!=null){
            mViewPresenter.onActivityResult(requestCode,resultCode,data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
